#include "message.hpp"
#include "net.hpp"
#include "dispatcher.hpp"
#include <thread>
using namespace zyb_rpc;
using namespace std;
// using ConnectionCallback = std::function<void(const BaseConnection::ptr &)>;
// using CloseCallback = std::function<void(const BaseConnection::ptr &)>;
// using MessageCallback = std::function<void(const BaseConnection::ptr &, BaseMessage::ptr &)>;
void onRpcResponse(const BaseConnection::ptr &conn, RpcResponse::ptr &msg)
{
    cout << "收到rpc响应：";
    string body = msg->serialize();
    cout << body << endl;
}
void onTopicResponse(const BaseConnection::ptr &conn, TopicResponse::ptr &msg)
{
    cout << "收到Topic响应：";
    string body = msg->serialize();
    cout << body << endl;
}
int main()
{
    Dispatcher::ptr dispather = make_shared<Dispatcher>();
    dispather->registerHandler<RpcResponse>(MType::RSP_RPC, onRpcResponse);       // 注册映射关系
    dispather->registerHandler<TopicResponse>(MType::RSP_TOPIC, onTopicResponse); // 注册映射关系

    BaseClient::ptr client = ClientFactory::create("127.0.0.1", 9090);
    MessageCallback message_cb = std::bind(&Dispatcher::onMessage, dispather.get(),
                                           placeholders::_1, placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();

    auto rpc_req = MessageFactory::create<RpcRequest>();
    rpc_req->setId("111");
    rpc_req->setMType(MType::REQ_RPC);
    rpc_req->setMethod("add");
    Json::Value param;
    param["num1"] = 1;
    param["num2"] = 2;
    rpc_req->setParams(param);
    client->send(rpc_req);

    TopicRequest::ptr topic_req = MessageFactory::create<TopicRequest>();
    topic_req->setId("111");
    topic_req->setMType(MType::REQ_TOPIC);
    topic_req->setTopicKey("news");
    client->send(topic_req);

    std::this_thread::sleep_for(std::chrono::seconds(10));
    client->shutdown();
    return 0;
}

// #include "message.hpp"
// #include "net.hpp"
// #include "dispatcher.hpp"
// #include <thread>
// using namespace zyb_rpc;
// using namespace std;
// // using ConnectionCallback = std::function<void(const BaseConnection::ptr &)>;
// // using CloseCallback = std::function<void(const BaseConnection::ptr &)>;
// // using MessageCallback = std::function<void(const BaseConnection::ptr &, BaseMessage::ptr &)>;
// void onMessage(const BaseConnection::ptr &conn, BaseMessage::ptr &msg)
// {
//     string body = msg->serialize();
//     cout << body << endl;
// }

// int main()
// {
//     BaseClient::ptr client = ClientFactory::create("127.0.0.1", 9090);
//     client->setMessageCallback(onMessage);
//     client->connect();
//     auto rpc_req = MessageFactory::create<RpcRequest>();
//     rpc_req->setId("111");
//     rpc_req->setMType(MType::RSP_RPC);
//     rpc_req->setMethod("add");
//     Json::Value param;
//     param["num1"] = 1;
//     param["num2"] = 2;
//     rpc_req->setParams(param);
//     client->send(rpc_req);
//     std::this_thread::sleep_for(std::chrono::seconds(10));
//     client->shutdown();
//     return 0;
// }